// pages/login/login.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
Page({
  data: {
    alertState: true,
    userInfo: {},
    account: '15000000000',
    password: '123456',
    comId: 0,
    title: "",
    tipMsg: '',
    loadingState: true
  },
  onShareAppMessage: function () {
    return {
      title: '美铺商家版',
      desc: '发现美，创造美，分享美',
      path: "pages/login/login"
    }
  },
  bindAccountInput: function (e) {
    this.setData({
      account: e.detail.value
    })
    console.log(e.detail.value)
  },
  bindPasswordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
    console.log(e.detail.value)
  },
  toastHide: function (event) {
    console.log("触发bindchange，隐藏toast")
    this.setData({ alertState: true })
  },
  loadingChange: function (event) {
    console.log("触发bindchange，隐藏loading")
    this.setData({ loadingState: true })
  },
  getCompany: function () {
    util.loading();
    var $this = this;
    ajax.P(
      app.config.apiUrl + '/openapi/Employee/getCompany',
      { account: this.data.account, passwd: this.data.password },
      function (res) {
        var d = res.data;
        if (d.code == 0) {
          if (d.result.count > 1) {
            util.showOK('有多个企业');
          } else {
            app.setComId(d.result.comid);
            $this.doLogin();
          }
        } else {
          util.alert(d.msg);
        }
      }
    );
  },
  //事件处理函数
  doLogin: function () {
    util.loading();
    var $this = this;
    ajax.P(
      app.config.apiUrl + '/openapi/Employee/login',
      {
        account: 15000000000,
        passwd: 123456
      },
      function (res) {
        var d = res.data;
        if (d.code == 0) {
          util.showOK('登录成功');
          app.setUserSid(d.result.sid);
          app.setComId(d.result.comid);
          setTimeout(function () {
            wx.redirectTo({
              url: '../business/index'
            });
          }, 1500)
        } else {
          util.alert(d.msg);
        }
      }
    );
  },
  openIdLogin: function (openid) {
    var $this = this;
    ajax.P(
      app.config.apiUrl + '/openapi/Employee/openIdLogin',
      {
        openId: openid
      },
      function (res) {
        var d = res.data;
        if (d.code == 0) {
          util.showOK('登录成功');
          app.setUserSid(d.result.sid);
          app.setComId(d.result.comid);

          setTimeout(function () {
            wx.redirectTo({
              url: '../business/index'
            });
          }, 1500)
        }
      }
    );
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var $this = this;
    wx.login({
      success: function (res) {
        ajax.G(
          app.config.apiUrl + '/openapi/Employee/getUserOpenID?jscode=' + res.code,
          function (r) {
            app.setOpenId(r.data.result.openid);
            $this.openIdLogin(r.data.result.openid);
          }
        );
      },
      fail: function () { },
      complete: function () { }
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

    // if (app.globalData.userSid != null) {
    //      wx.redirectTo({
    //               url: '../business/index'
    //           });
    //           return ;
    // }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭

    // pushHistory();  
    // setTimeout(function () {  
    //   window.addEventListener("popstate", function(e) {  
    //     showBox("再按一次退出程序", 2000, function() {          
    //       pushHistory();  
    //     });  
    //   }, false);  
    // }, 300);  

    // function pushHistory() {  
    //   var state = {  
    //     title: "title",  
    //     url: "#"  
    //   };  
    //   window.history.pushState(state, "title", "#");  
    // }  

    function showBox(msg, timeOut, onTimeOut) {
      if (typeof alertBoxDiv === "undefined") {
        alertBoxDiv = $("<div/>").addClass("alert-box hide").append($("<div/>").addClass("label label-primary")).appendTo($("body"));
      }
      alertBoxDiv.children(".label").html(msg);
      alertBoxDiv.removeClass("hide");
      if (typeof timeOut === "undefined") timeOut = 2000;
      setTimeout(function () {
        alertBoxDiv.addClass("hide");
        if (typeof onTimeOut !== "undefined") onTimeOut();
      }, timeOut);
    }
  }
})